package org.zhuxian.cn.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.zhuxian.cn.entity.User;
import org.zhuxian.cn.entity.VO.UserVO;
import org.zhuxian.cn.mapper.UserMapper;
import org.zhuxian.cn.service.UserService;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public boolean registerUser(User user) {
        // 检查用户名是否已存在
        User existingUser = userMapper.findByUsername(user.getUsername());
        if (existingUser == null) {
            // 用户名不存在，进行注册
            // 对密码进行 BCrypt 加密
            user.setPassword(passwordEncoder.encode(user.getPassword()));
            // 插入新用户记录
            userMapper.insert(user);
            return true;
        } else {
            return validateUser(user.getUsername(),user.getPassword());
        }

    }

    @Override
    public boolean validateUser(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user != null && user.getStatus() == 1) {
            return passwordEncoder.matches(password, user.getPassword());
        }
        return false;
    }

    @Override
    public UserVO getUserByUsername(String username) {
        User user = userMapper.findByUsername(username);
        UserVO userVO = new UserVO();
        if (user != null) {
            BeanUtils.copyProperties(user, userVO);
        }
        return userVO;
    }

    @Override
    public boolean deleteUser(Long id) {
        return userMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateUser(User user) {
        User existingUser = userMapper.selectById(user.getId());
        if (existingUser != null) {
            // 如果更新包含密码，需要重新加密
            if (user.getPassword() != null && !user.getPassword().isEmpty()) {
                user.setPassword(passwordEncoder.encode(user.getPassword()));
            } else {
                user.setPassword(existingUser.getPassword());
            }
            return userMapper.updateById(user) > 0;
        }
        return false;
    }

    @Override
    public UserVO getUserById(Long id) {
        User user = userMapper.selectById(id);
        UserVO userVO = new UserVO();
        if (user != null) {
            BeanUtils.copyProperties(user, userVO);
        }
        return userVO;
    }

    @Override
    public List<UserVO> getAllUsers() {
        List<User> users = userMapper.selectList(null);
        return users.stream()
                .map(user -> {
                    UserVO userVO = new UserVO();
                    BeanUtils.copyProperties(user, userVO);
                    return userVO;
                })
                .collect(Collectors.toList());
    }
}
